﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using KaraokeManagementBUS;
using KaraokeManagementDTO;

namespace KaraokeManagement
{
    public partial class ucQuanLyHangHoa : UserControl
    {
        public static int staticMaHangHoa = -1;

        public ucQuanLyHangHoa()
        {
            InitializeComponent();
            
        }

        private void btnLapPhieuNhapKho_Click(object sender, EventArgs e)
        {
            frmPhieuNhapKho frm = new frmPhieuNhapKho();
            frm.ShowDialog();
            loadDanhSachHangHoa();
        }


        public void initNhomHangHoa()
        {
            cbNhomHangHoa.DataSource = new NhomHangHoaBUS().layDanhSachNhomHangHoa();
            cbNhomHangHoa.DisplayMember = "TenNhomHangHoa";
            cbNhomHangHoa.ValueMember = "ID";
        }


        public void initDonViTinh()
        {
            cdDonViTinh.DataSource = new DonViTinhBUS().layDanhSachDonViTinh();
            cdDonViTinh.DisplayMember = "TenDonViTinh";
            cdDonViTinh.ValueMember = "ID";

        }

        private void ucQuanLyHangHoa_Load(object sender, EventArgs e)
        {
            //dgvDanhSachHangHoa.AutoGenerateColumns = false;
            initNhomHangHoa();
            initDonViTinh();
            loadDanhSachHangHoa();
        }

        public void loadDanhSachHangHoa()
        {
            
            //dgvDanhSachHangHoa.Rows.Clear();
            dgvDanhSachHangHoa.DataSource = new HangHoaBUS().layDanhSachHangHoa();
            //dgvDanhSachHangHoa.Refresh();
          

        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có chắc không ?","Cảnh báo xóa",MessageBoxButtons.YesNo) == DialogResult.No)
            {
                return;
            }

            if (dgvDanhSachHangHoa.SelectedRows.Count > 0)
            {
                int MaHangHoa = int.Parse(dgvDanhSachHangHoa.SelectedRows[0].Cells["ID"].Value.ToString());
                if (new HangHoaBUS().xoaHangHoa(MaHangHoa))
                {
                    MessageBox.Show("Xóa thành công !", "Karaoke");
                    loadDanhSachHangHoa();
                }
                else
                {
                    MessageBox.Show("Xóa thất bại !", "Karaoke");
                }
            }
        }

        private void btnCapNhat_Click(object sender, EventArgs e)
        {
            if (dgvDanhSachHangHoa.SelectedRows.Count > 0)
            {
                staticMaHangHoa = int.Parse(dgvDanhSachHangHoa.SelectedRows[0].Cells["ID"].Value.ToString());
                frmPhieuNhapKho frm = new frmPhieuNhapKho();
                frm.ShowDialog();

                loadDanhSachHangHoa();
                
            }
        }



        
    }
}
